angular.module('imsApp').controller('advManagementAddModalController', ['$scope','$state', 'AdvManagementManager','mode','$uibModalInstance','AdvertisersManager','$timeout',function ($scope,$state, AdvManagementManager,mode,$uibModalInstance,AdvertisersManager,$timeout) {
    $scope.advManagement={type:410};
    $scope.ok = function () {
        saveAdvManagementData();

    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss("cancel");
    };
    $scope.rangeDate = {
        startDate:null,
        endDate: null,
        onSelected: function (data, scope) {  //时间选择确认事件
            var startDay = moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD");
            var endDay = moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD");
            $scope.formData.startEndDay=startDay+"~" +endDay
        }
    };
   AdvertisersManager.advertiserSimpleList([1,1000]).then(function(result){
        $scope.advList=result.data.pageList;
    })

    $scope.submited=false;
    var saveAdvManagementData=function () {
        $scope.submited=true;

        if($scope.formData.advertiserId){
                if($scope.formData.campaignName){
                    if( $scope.formData.startEndDay){
                        AdvManagementManager.campaignsAdd($scope.formData).then(function (result) {
                            if(result.data.c=='s'){
                                $uibModalInstance.close("ok");
                            }else{
                                $uibModalInstance.close("error");
                            }
                        });
                    }
                }
        }
    }
    $scope.functionThatReturnsPromise = function() {
        return $timeout(angular.noop, 1000);//这里可以设置时间，改文字可以删除
    }

}])
angular.module('imsApp').directive('clickAndDisable', function() {
    return {
        scope: {
            clickAndDisable: '&'
        },
        link: function(scope, iElement, iAttrs) {
            iElement.bind('click', function() {
                iElement.prop('disabled',true);
                scope.clickAndDisable().finally(function() {
                    iElement.prop('disabled',false);
                })
            });
        }
    };
});

